module.exports = (sequelize, DataTypes) => {
  const KnowledgePoint = sequelize.define('KnowledgePoint', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    name: {
      type: DataTypes.STRING(100),
      allowNull: false,
      comment: '知识点名称'
    },
    parentId: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '父节点ID，构建层级树'
    },
    courseId: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: '关联课程ID'
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: '知识点描述'
    },
    difficulty: {
      type: DataTypes.ENUM('easy', 'medium', 'hard'),
      allowNull: true,
      comment: '难度级别'
    },
    importance: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 5,
      comment: '重要性评分(1-10)'
    }
  }, {
    tableName: 'knowledge_points',
    timestamps: true
  });

  KnowledgePoint.associate = (models) => {
    // 知识点自关联，构建树形结构
    KnowledgePoint.belongsTo(KnowledgePoint, {
      foreignKey: 'parentId',
      as: 'parent'
    });

    KnowledgePoint.hasMany(KnowledgePoint, {
      foreignKey: 'parentId',
      as: 'children'
    });
  };

  return KnowledgePoint;
};